<script setup>
import { ref, nextTick } from 'vue'
import { itemPageQueryService } from '@/api/item.js'
import { Search, Refresh } from '@element-plus/icons-vue'
import ItemCategorySelect from '../../views/item/components/ItemCategorySelect.vue'

// 控制弹窗显示
const visibleDialog = ref(false)

const itemSelectTitle = ref('选择物品')
const itemList = ref([]) // 物品列表
const total = ref(0) // 总条数
const loading = ref(false) // loading状态

// 表格选中项
const emit = defineEmits(['add']) // 定义自定义事件
const selectedItems = ref([])
const tableRef = ref()

// 请求参数
const params = ref({
  pageNum: 1, // 当前页码
  pageSize: 10, // 每页条数
  orderBy: 'update_time', // 排序字段
  isAsc: false, // 是否升序
  name: null, // 物品名称
  itemCategoryId: null, // 物品类别id
  brand: null, // 品牌
  status: null // 物品状态
})

// 基于params参数，获取物品列表
const getItemList = async () => {
  loading.value = true
  const res = await itemPageQueryService(params.value)
  itemList.value = res.data.data.list
  total.value = res.data.data.total
  loading.value = false
}
getItemList()

// 处理分页逻辑
const onPageSizeChange = (size) => {
  params.value.pageNum = 1
  params.value.pageSize = size
  getItemList()
}
const onPageNumChange = (page) => {
  params.value.pageNum = page
  getItemList()
}

// 搜索逻辑
const handleSearch = () => {
  params.value.pageNum = 1
  getItemList()
}

// 重置逻辑
const handleReset = () => {
  params.value = {
    ...params.value, // 保留其他属性
    pageNum: 1,
    name: null,
    itemCategoryId: null,
    brand: null,
    status: null
  }
  getItemList()
}

// 处理选中项变化
const handleSelectionChange = (selection) => {
  selectedItems.value = selection
}

// 添加选中物品
const handleAddSelected = () => {
  // 转换为父组件需要的结构
  emit('add', selectedItems.value) // 触发事件
  visibleDialog.value = false
}

// 向外暴露方法
const open = (title) => {
  visibleDialog.value = true // 打开抽屉
  itemSelectTitle.value = title
  handleReset()
  // 清空选中状态
  nextTick(() => {
    tableRef.value?.clearSelection()
  })
}

defineExpose({
  open
})
</script>

<template>
  <el-dialog v-model="visibleDialog" width="60%">
    <page-container :title="itemSelectTitle">
      <template #extra>
        <el-button type="primary" plain @click="handleAddSelected">添加已选中物品</el-button>
      </template>
      <!-- 表单 -->
      <el-form inline>
        <el-form-item label="物品名称:">
          <el-input v-model="params.name" style="width: 100px" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="物品类别:">
          <item-category-select v-model="params.itemCategoryId" width="150px" />
        </el-form-item>
        <el-form-item label="品牌:">
          <el-input v-model="params.brand" style="width: 100px" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="物品状态:">
          <el-select v-model="params.status" style="width: 100px">
            <el-option label="禁用" value="禁用"></el-option>
            <el-option label="启用" value="启用"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch"></el-button>
          <el-button type="default" :icon="Refresh" @click="handleReset"></el-button>
        </el-form-item>
      </el-form>
      <!-- 表格 -->
      <el-table
        ref="tableRef"
        @selection-change="handleSelectionChange"
        row-key="id"
        class="tb"
        :data="itemList"
        v-loading="loading"
        :row-style="{ height: '50px' }"
      >
        <el-table-column type="selection" label="选择" fixed width="50" />
        <el-table-column prop="id" label="物品ID" width="100" />
        <el-table-column prop="name" label="物品名称" width="120" />
        <el-table-column prop="picUrl" label="物品图片" width="100">
          <template #default="{ row }">
            <el-image style="width: 50px; height: 50px" :src="row.picUrl" />
          </template>
        </el-table-column>
        <el-table-column prop="itemCategoryName" label="物品类别" width="120" />
        <el-table-column prop="brand" label="品牌" width="120" />
        <el-table-column prop="price" label="物品价格" width="120">
          <template #default="{ row }"> {{ (row.price / 100).toFixed(2) }}元 </template>
        </el-table-column>
        <el-table-column prop="stock" label="库存" width="80" />
        <el-table-column prop="minStock" label="最小库存" width="80" />
        <el-table-column prop="status" label="物品状态" width="100">
          <template #default="{ row }">
            <el-switch
              v-model="row.status"
              inline-prompt
              style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
              active-value="启用"
              inactive-value="禁用"
              active-text="启用"
              inactive-text="禁用"
              disabled
            />
          </template>
        </el-table-column>
        <el-table-column prop="description" label="物品描述" width="150" show-overflow-tooltip />
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column prop="updateTime" label="更新时间" width="180" />
        <!-- 空数据提示 -->
        <template #empty>
          <el-empty description="没有数据"></el-empty>
        </template>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        v-model:current-page="params.pageNum"
        v-model:page-size="params.pageSize"
        :page-sizes="[5, 10, 20, 50, 100]"
        :background="true"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="onPageSizeChange"
        @current-change="onPageNumChange"
        style="margin-top: 20px; justify-content: flex-end"
      />
    </page-container>
  </el-dialog>
</template>

<style lang="scss" scoped>
.tb {
  border-radius: 10px;
  box-shadow: 0 5px 10px 0 rgba(0, 0, 0, 0.05);
  overflow: hidden;
}
</style>
